<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :keyboard="false"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 100%;overflow-y: hidden">
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="仓库名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="请选择仓库" v-decorator.trim="['warehouseName', validatorRules.warehouseName]"  :dropdownMatchSelectWidth="false"
           showSearch optionFilterProp="children" @change="changeSelect" >
            <a-select-option v-for="(item,index) in warehouseList" :key="index" :value="item.name">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item  label="材料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入材料名称" v-decorator.trim="[ 'name', validatorRules.name]" />
        </a-form-item>
        <a-form-item label="材料类型" :labelCol="labelCol" :wrapperCol="wrapperCol" >
           <a-select placeholder="请选择材料类型" v-decorator.trim="[ 'type', validatorRules.type]" :dropdownMatchSelectWidth="false" 
           showSearch optionFilterProp="children" @change="changeSelect" >
                      <a-select-option  value="辅料">辅料 </a-select-option>
                      <a-select-option  value="原材料" > 原材料 </a-select-option>
            </a-select>
        </a-form-item>
        <a-form-item  label="材料数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
                   <a-input-number placeholder="请输入材料数量" style="width: 100%" :min="1" v-decorator.trim="[ 'num', validatorRules.num]" />
        </a-form-item>
           
        <a-form-item  label="材料单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <a-select placeholder="" v-decorator.trim="[ 'unit', validatorRules.unit]" :dropdownMatchSelectWidth="false"  @change="changeSelect" >
                      <a-select-option  value="件" >件 </a-select-option>
                      <a-select-option  value="个" > 个 </a-select-option>
                      <a-select-option  value="吨" > 吨 </a-select-option>
            </a-select>
        </a-form-item>
        <a-form-item label="材料描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea :rows="3" placeholder="请输入材料描述" v-decorator="[ 'description' ]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import Vue from 'vue'
  import JSelectPosition from '@/components/jeecgbiz/JSelectPosition'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import {addStorage,editStorage} from '@/api/api'
  import JSelectMultiple from '@/components/jeecg/JSelectMultiple'
  import JImageUpload from '../../../components/jeecg/JImageUpload'
  import {StorageListMixin } from '../mixins/StorageListMixin'
  export default {
    name: "StorageModal",
    mixins:[StorageListMixin],
    components: {
      JImageUpload,
      JSelectMultiple,
      JSelectPosition
    },
    data () {
      return {
        title:"操作",
        visible: false,
        modalWidth:600,
        drawerWidth:700,
        id:"", //保存工程id
        isReadOnly: false,
        disableSubmit:false,
        dateFormat:"YYYY-MM-DD",
        validatorRules:{
          
          wareHouseName:{
            rules: [{
              required: true, message: '请选择仓库名称!'
            }]
          },
          name:{
            rules: [{
              required: true, message: '请输入材料名称!'
            }]
          }
        },
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        uploadLoading:false,
        confirmLoading: false,
        headers:{},
        form:this.$form.createForm(this)
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token};
      this.initWareHouse()
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        let that = this;
        that.form.resetFields();
        that.id = record.id;
        that.visible = true;
        that.model = Object.assign({}, record);
        that.$nextTick(() => {
          that.form.setFieldsValue(pick(this.model,'warehouseName','name','type','num','unit','description'))
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
        this.disableSubmit = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            let obj;
            if(!this.model.id){
              formData.id = this.id;
              obj=addStorage(formData);
            }else{
              obj=editStorage(formData);
            }
            obj.then((res)=>{
              if(res.code === 200){
                that.$emit('ok');
              }else{
                that.$message.warning(res.data.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel() {
        this.close()
      }
    }
  }
</script>

<style scoped>

</style>